<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('仓库出库详情列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li class="type">
                                <label>出库类型：</label>
                                <select name="type" th:with="type=${@dict.getType('b_sell_type')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>出库单号：</label>
                                <input type="text" name="sellDetailNo"/>
                            </li>
                            <li hidden="hidden">
                                <label>仓库ID：</label>
                                <input type="text" name="warehouseId"/>
                            </li>
                            <li>
                                <label>开始时间：</label>
                                <input type="text" class="time-input" placeholder="请选择开始时间" name="params[startDate]"/>
                            </li>
                            <li>
                                <label>结束时间：</label>
                                <input type="text" class="time-input" placeholder="请选择结束时间" name="params[endDate]"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="sellStoreDetail:sellStoreDetail:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="sellStoreDetail:sellStoreDetail:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-success" onclick="addAccount()">
                    <i class="fa fa-plus"></i> 对账单
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="sellStoreDetail:sellStoreDetail:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning hidden" onclick="$.table.exportExcel()" shiro:hasPermission="sellStoreDetail:sellStoreDetail:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
     <th:block th:include="include :: bootstrap-table-print-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('sellStoreDetail:sellStoreDetail:edit')}]];
        var removeFlag = [[${@permission.hasPermi('sellStoreDetail:sellStoreDetail:remove')}]];
        var typeDatas = [[${@dict.getType('b_sell_type')}]];
        var prefix = ctx + "warehouse/sellStoreDetail";
        let sellStoreDetail = [[${sellStoreDetail}]];
        let  url= prefix + "/list";
        let isContractDefault=false;
        if(!isEmptyString(sellStoreDetail.warehouseId) && !isEmptyString(sellStoreDetail.productId)){
            //仓库查询
            url=url+"?warehouseId="+sellStoreDetail.warehouseId+"&productId="+sellStoreDetail.productId;
        }else if(!isEmptyString(sellStoreDetail.contractId)){
            if(sellStoreDetail.contractId == contractDefault.id){
                //直接出库
                url=url+"?contractId="+sellStoreDetail.contractId
                $(".type").addClass("hidden");
                isContractDefault=true;
            }
            if(!isEmptyString(sellStoreDetail.accountId)){
                //合同查询
                url=url+"?contractId="+sellStoreDetail.contractId+"&accountId="+sellStoreDetail.accountId;
            }

        }else if(!isEmptyString(sellStoreDetail.accountId)){
            //对账单查询
            url=url+"?accountId="+sellStoreDetail.accountId;
        }else if(!isEmptyString(sellStoreDetail.orderId)){
            //订单查询
            url=url+"?orderId="+sellStoreDetail.orderId;
        } else if(!isEmptyString(sellStoreDetail.orderDetailId)){
            //订单详情查询
            url=url+"?orderDetailId="+sellStoreDetail.orderDetailId;
        }else{
            hiddenAddBtn();
        }

        if( sellStoreDetail.contractId !=contractDefault.id || !isEmptyString(sellStoreDetail.orderDetailId)){
            hiddenAddBtn()
        }

        function hiddenAddBtn(){
            $(".btn-success").addClass("hidden");
        }
        $(function() {
            var options = {
                url: url,
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                sortName: "sellDetailId",
                sortOrder:"desc",
                showPrint: true,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: true,
                modalName: isEmptyString(sellStoreDetail.orderDetailId)?"直接出库":"仓库出库详情",
                printPageBuilder: printPageBuilder,
                columns: [{checkbox: true,  printIgnore: true},
                    {
                        field: 'sellDetailId',
                        title: 'ID',
                        visible: false
                    },
                    {
                        field: 'sellDetailNo',
                        title: '出库单号'
                    },
                    {
                        field: 'type',
                        title: '入库类型',
                        visible: !isContractDefault,
                        formatter: function(value, row, index) {
                            return $.table.selectDictLabel(typeDatas, value);
                        }
                    },
                    {
                        field: 'productId',
                        title: '产品ID',
                        visible: false
                    },
                    {
                        field: 'name',
                        title: '产品名称',
                    },
                    {
                        field: 'code',
                        title: '产品编码',
                    },
                    {
                        field: 'unit',
                        title: '产品单位'
                    },
                    {
                        field: 'price',
                        title: '产品价格',
                    },
                    {
                        field: 'averageMoney',
                        title: '成本价格',
                    },
                    {
                        field: 'num',
                        title: '产品出库数量'
                    },
                    {
                        field: 'supplierName',
                        title: '合作伙伴',
                    },
                    {
                        field: 'createBy',
                        title: '创建者',
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                    },
                    {
                        field: 'remark',
                        title: '备注',
                    },
                 /*   {
                        title: '操作',
                        align: 'center',
                        visible: false,
                        printIgnore: true,
                        formatter: function(value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.storeDetailId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.storeDetailId + '\')"><i class="fa fa-remove"></i>删除</a>');
                            return actions.join('');
                        }
                    }*/
                    ]
            };
            $.table.init(options);
        });


        // 自定义打印页面模板

        function selectColumnParams() {
            var column = $.table.selectColumns('sellDetailId');
            if (column == null|| column.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            let obj = {
                searchValue: JSON.stringify(column),
                totalMoney: getTotalMoney(),
                startDate:getStartDateParams(),
                endDate:getEndDateParams(),
            };
            //alert(column);
            return obj;
        }

        function getTotalMoney() {
            var column = $("#bootstrap-table").bootstrapTable('getSelections');
            //var column = $.table.selectColumns('price');
            if (column == null|| column.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            var totalMoney = 0;
            for (var i = 0; i < column.length; i++) {
                totalMoney = totalMoney + parseFloat(column[i].price * column[i].num);
            }
            return totalMoney;
        }

        function getStartDateParams() {
            var startDate = $("input[name='params[startDate]']").val();
            if(isEmptyString(startDate)){
                let column = $.table.selectColumns('createTime');
                startDate=column[0];
            }
            return startDate

        }
        function getEndDateParams() {
            var endDate = $("input[name='params[startDate]']").val();
            if(isEmptyString(endDate)){
                let column = $.table.selectColumns('createTime');
                endDate=column[column.length-1];
            }
            return endDate

        }

        function addAccount(){
            openWin("对账单", ctx + "account/account?contractId="+contractDefault.id+"&type="+contractTypeObj.sell)
        }
    </script>
</body>
</html>
